package cn.swunet.utils;

import java.io.UnsupportedEncodingException;

import cn.swunet.bean.User;
import cn.swunet.dao.UserDao;
import cn.swunet.schat.service.UserManagerWorker;

public class PacketUtils {
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:byte[] ack packet header 
	 * @TODO:
	 */
	public static byte[] getAckHeader(byte[] DesUserId,byte[] desIp, byte[] desPort) {
		return getHeader(DesUserId, desIp, desPort, 8); 
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:byte[] Ack packet header
	 * @TODO: 返回指定的packet header ---> uid host
	 */
	public static byte[] getHeader(byte[] DesUserId,byte[] desIp, byte[] desPort, int type) {
		byte[] data = new byte[17]; 
		data[0] = (byte)0;
		data[1] = (byte)17;
		//type
		data[2] = (byte)type;
		//DesUID
		System.arraycopy(DesUserId, 0, data, 3, 4);
		//DesIp
		System.arraycopy(desIp, 0, data, 7, 4);
		//DesPort
		System.arraycopy(desPort, 0, data, 11, 2);
		//srcUId = 0		
		return data;
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:byte[]	Nack packet header
	 * @TODO:
	 */
	public static byte[] getNackHeader(byte[] DesUserId,byte[] desIp, byte[] desPort) {
		return getHeader(DesUserId, desIp, desPort, 9); 
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:byte[]
	 * @TODO: 组装header、data--->packet
	 */
	public static byte[] constructPacket(byte[] header, byte[] data) {
		byte[] packet = new byte[header.length+data.length];
		System.arraycopy(header, 0, packet, 0, header.length);
		System.arraycopy(data, 0, packet, header.length, data.length);
		//packet size
		packet[0] = (byte)((packet.length >> 8) & 0xff);
		packet[1] = (byte)(packet.length & 0xff);
		return packet;
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:byte[]
	 * @TODO:组装header、data--->packet String-->byte use utf-8 encoding
	 */
	public static byte[] constructPacket(byte[] header, String data){
		byte[] b_data = null;
		try {
			b_data = data.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			System.out.println("constructPacket error: charset coding error!");
		}
		byte[] packet = new byte[header.length+b_data.length];
		System.arraycopy(header, 0, packet, 0, header.length);
		System.arraycopy(data, 0, packet, header.length, b_data.length);
		//packet size
		packet[0] = (byte)((packet.length >> 8) & 0xff);
		packet[1] = (byte)(packet.length & 0xff);
		return null;
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:String
	 * @TODO: 将data转换为String返回
	 */
	public static String getDataToString(byte[] data, int length) {
		return new String(data, 17, length);
	}
	
	/**
	 * 
	 * @author:arosen.swunet@qq.com
	 * @return:boolean
	 * @TODO:验证用户 添加ip port记录
	 */
	public static synchronized boolean verifyUser(String uname, String upasswd, int srcu_id, String srcIp, int port) {
		boolean revt = false;
		UserDao userDao = new UserDao();
		User user = userDao.getUserByName(uname);
		if (user != null && user.getPasswd().equals(upasswd)) {
			user.setActiveIp(srcIp);
			user.setActivePort(port);
			user.setLastActive(0);
			UserManagerWorker.addOnlineUser(user);
			revt = true;
		}
		
		return revt;
	}
}
